package com.project.jdbc;

import com.project.bean.UserBean;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao{
    public void add(UserBean user){
        this.setConnection();

        try {
            ps = con.prepareStatement("insert into t_user(u_name,u_birthday) values(?,?)");
            ps.setObject(1,user.getName());
            ps.setObject(2,user.getBirthday());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConection();
        }
    }

    public List<UserBean> findAll(){
        List<UserBean> list = new ArrayList<>();
        this.setConnection();

        try {
            ps = con.prepareStatement("select * from t_user");
            rs = ps.executeQuery();
            while(rs.next()){
                UserBean user = new UserBean(rs.getString("u_name"),
                        LocalDate.parse(rs.getString("u_birthday")));
                list.add(user);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConection();
        }

        return list;
    }

    public static void main(String[] args) {
        UserDao dao = new UserDao();
//        dao.add(new UserBean("石井英明", LocalDate.parse("1992-03-04")));
        System.out.println(dao.findAll());
    }
}
